<?php

// 管理员管理

namespace app\console\controller;

use think\Url;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\Manager as ManagerModel;
use app\console\model\AuthGroup as AuthGroupModel;
use think\Cache;
use think\Db;

class Manager extends Controller
{

    /**
     * 用户列表
     */
    public function index(Request $request, ManagerModel $manager)
    {


        // 获取参数
        $inquire = $request->instance()->param();

        // 搜索条件
        $condition['id'] = ['gt', 0];

        if (!empty($inquire['title'])) $condition['username|nickname|telephone'] = ['like', '%' . $inquire['title'] . '%'];

        $data = $manager->where($condition)->order(['id' => 'desc'])->paginate(10, false, ['query' => $inquire]);


        return $this->fetch('', [
            "mate_title" => '用户列表',
            'data'       => $data
        ]);

    }

    /**
     * @return array|mixed|\think\response\View
     * 管理员发放会员卡
     */
    public function manage_send_free_card()
    {
        $param = Request::instance()->param();
        $where = [];
        if(!empty($param['title'])) $where['username'] = ['like','%'.$param['title'].'%'];
        $data = Db::name('manager')
            ->alias('m')
            ->join('auth_group l','m.level = l.id')
            ->field('m.*,l.title')
            ->where('level = 2 or level = 5')
            ->order('level','asc')
            ->where($where)
            ->paginate(10,false,['query' => $param]);
        return view('',['data' => $data]);
    }
    /**
     * 栏目状态改变
     */
    public function status($id, Request $request)
    {
        $id = $request->instance()->param('id');
        $status = Db::name('manager')->where('id', $id)->value('is_allow') == '1' ? 2 : 1;
        if (Db::name('manager')->where('id', $id)->update(['is_allow' => $status])) {
            return $status;
        }
        return false;
    }


    /**
     * 用户添加
     */
    public function create(Request $request, ManagerModel $manager, AuthGroupModel $authGroup)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();
            // 验证
            $validate = Loader::validate('Manager');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $manager->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('Manager/index'));
            }

            fail('操作失败');

        }

        // 读取权限等级
        $group = $authGroup::all();
        $salesman = Db::name('salesman s')
            ->join('manager m','s.manage_id = m.id','left')
            ->field('m.username,m.nickname,m.create_time,s.*,m.id,m.telephone')
            ->where('m.level',4)
            ->select()->toArray();
        $salesman = getMenuTree2($salesman,0);
        return $this->fetch('', [
            "mate_title" => '用户添加',
            'group'      => $group,
            'salesman' => $salesman

        ]);

    }


    /**
     * 用户编辑
     */
    public function update($id, Request $request, ManagerModel $manager, AuthGroupModel $authGroup)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('Manager');
            if (!$validate->scene('update')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $manager->allowField(true)->save($data, ['id' => $id]);


            if ($dispose !== false) {
                win('操作成功', Url::build('Manager/index'));
            }

            fail('操作失败');

        }

        $find = $manager::where('m.id',$id)
            ->alias('m')
            ->join('shop s','m.id = s.shop_manage_id','left')
            ->field('s.*,m.*')
            ->find(); //门店管理员
        // 读取权限等级
        $group = $authGroup::all();

        $salesman = Db::name('salesman s')
            ->join('manager m','s.manage_id = m.id','left')
            ->field('m.username,m.nickname,m.create_time,s.*,m.id,m.telephone')
            ->where('m.level',4)
            ->select()->toArray();
        $salesman = getMenuTree2($salesman,0);
        $find['salesman_id'] = Db::name('salesman')->where('manage_id',$id)->value('parent_id');
        return $this->fetch('create', [
            "mate_title" => '用户编辑',
            'data'       => $find,
            'group'      => $group,
            'salesman' => $salesman

        ]);
    }

    /*
     * 用户删除
     */
    public function destroy($id, ManagerModel $manager)
    {
        if ($manager->destroy($id)) {

            win('操作成功', Url::build('Manager/index'));
        }

        fail('操作失败');

    }


    /*
     * 状态改变
     */
    public function auditing(ManagerModel $manager)
    {
        $id = Request::instance()->param('id');

        $status = $manager->where('id', $id)->value('status') == '1' ? 2 : 1;

        if ($manager->where('id', $id)->update(['status' => $status])) {
            return $status;
        }
        return false;
    }
}
